package com.example.schooljava.util;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.servlet.http.HttpServletResponse;

/**
 * 结果类
 *
 * @author: LDH
 * @date: 2022年05月14日 10:21
 */
@Data
public class Result<T> {
    @ApiModelProperty("请求结果")
    private boolean success;
    @ApiModelProperty("请求返回提示")
    private String message;
    @ApiModelProperty("请求状态码")
    private int code;
    @ApiModelProperty("返回数据")
    private T data;
    @ApiModelProperty("时间戳")
    private String timestamp;
    @ApiModelProperty("token")
    private String token;

    public Result Huoqu_OK(T data){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(HttpServletResponse.SC_OK);
        result.setMessage("获取成功！");
        result.setData(data);
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        return result;
    }

    public Result OK(Boolean b,String message){
        Result result = new Result();
        result.setSuccess(b);
        result.setCode(HttpServletResponse.SC_OK);
        result.setMessage(message);
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        return result;
    }

    public Result OK_Back(Boolean b, String message, T data){
        Result result = new Result();
        result.setSuccess(b);
        result.setCode(HttpServletResponse.SC_OK);
        result.setMessage(message);
        result.setData(data);
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        return result;
    }

    public Result GetToken(T data, String token){
        Result result = new Result();
        result.setSuccess(true);
        result.setData(data);
        result.setCode(HttpServletResponse.SC_OK);
        result.setMessage("登录成功！");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setToken(token);
        return result;
    }

    public Result error(T data, String msg, int code){
        Result result = new Result();
        result.setSuccess(false);
        result.setData(data);
        result.setCode(code);
        result.setMessage(msg);
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        return result;
    }

}